<template>
  <Teleport to="body">
    <div 
      v-if="showWarning" 
      class="compatibility-warning-overlay"
      @click="handleOverlayClick"
    >
      <div class="compatibility-warning-modal" @click.stop>
        <div class="warning-header">
          <h2 class="warning-title">
            <span class="warning-icon">⚠️</span>
            浏览器兼容性警告
          </h2>
          <button 
            class="close-button"
            @click="dismissWarning"
            aria-label="关闭警告"
          >
            ×
          </button>
        </div>
        
        <div class="warning-content">
          <div class="browser-info">
            <h3>当前浏览器信息</h3>
            <p>
              <strong>浏览器：</strong>{{ browserInfo.name }} {{ browserInfo.version }}<br>
              <strong>操作系统：</strong>{{ browserInfo.os }}<br>
              <strong>设备类型：</strong>{{ browserInfo.isMobile ? '移动设备' : '桌面设备' }}
            </p>
          </div>
          
          <div class="compatibility-level">
            <h3>兼容性等级</h3>
            <div class="level-indicator" :class="compatibilityLevelClass">
              <span class="level-text">{{ compatibilityLevelText }}</span>
              <div class="level-bar">
                <div class="level-fill" :style="{ width: compatibilityPercentage + '%' }"></div>
              </div>
            </div>
          </div>
          
          <div v-if="warnings.length > 0" class="warnings-section">
            <h3>检测到的问题</h3>
            <ul class="warnings-list">
              <li v-for="warning in warnings" :key="warning" class="warning-item">
                {{ warning }}
              </li>
            </ul>
          </div>
          
          <div v-if="recommendations.length > 0" class="recommendations-section">
            <h3>建议</h3>
            <ul class="recommendations-list">
              <li v-for="recommendation in recommendations" :key="recommendation" class="recommendation-item">
                {{ recommendation }}
              </li>
            </ul>
          </div>
          
          <div class="supported-browsers">
            <h3>推荐浏览器</h3>
            <div class="browser-grid">
              <div class="browser-item">
                <span class="browser-icon">🌐</span>
                <span>Chrome 90+</span>
              </div>
              <div class="browser-item">
                <span class="browser-icon">🦊</span>
                <span>Firefox 88+</span>
              </div>
              <div class="browser-item">
                <span class="browser-icon">🧭</span>
                <span>Safari 14+</span>
              </div>
              <div class="browser-item">
                <span class="browser-icon">🔷</span>
                <span>Edge 90+</span>
              </div>
            </div>
          </div>
        </div>
        
        <div class="warning-actions">
          <button 
            class="action-button secondary"
            @click="continueAnyway"
          >
            仍要继续
          </button>
          <button 
            class="action-button primary"
            @click="refreshPage"
          >
            刷新页面
          </button>
        </div>
        
        <div class="warning-footer">
          <label class="dont-show-again">
            <input 
              type="checkbox" 
              v-model="dontShowAgain"
            >
            不再显示此警告
          </label>
        </div>
      </div>
    </div>
  </Teleport>
</template>

<script setup lang="ts">
import { ref, computed, onMounted } from 'vue'
import { browserCompatibility, type CompatibilityReport } from '@/utils/browserCompatibility'

/**
 * 兼容性警告组件
 * 当检测到浏览器兼容性问题时显示警告对话框
 * @component
 * @example
 * <CompatibilityWarning />
 */

// 响应式数据
const showWarning = ref(false)
const dontShowAgain = ref(false)
const compatibilityReport = ref<CompatibilityReport | null>(null)

// 计算属性
const browserInfo = computed(() => compatibilityReport.value?.browser || {
  name: 'Unknown',
  version: 'Unknown',
  os: 'Unknown',
  isMobile: false
})

const warnings = computed(() => compatibilityReport.value?.warnings || [])
const recommendations = computed(() => compatibilityReport.value?.recommendations || [])

const compatibilityLevelClass = computed(() => {
  const level = compatibilityReport.value?.compatibilityLevel
  return `level-${level}`
})

const compatibilityLevelText = computed(() => {
  const level = compatibilityReport.value?.compatibilityLevel
  switch (level) {
    case 'excellent': return '优秀'
    case 'good': return '良好'
    case 'fair': return '一般'
    case 'poor': return '较差'
    default: return '未知'
  }
})

const compatibilityPercentage = computed(() => {
  const level = compatibilityReport.value?.compatibilityLevel
  switch (level) {
    case 'excellent': return 95
    case 'good': return 75
    case 'fair': return 55
    case 'poor': return 25
    default: return 0
  }
})

/**
 * 检查是否应该显示警告
 * 根据兼容性检测结果和用户设置决定是否显示警告
 */
function checkShouldShowWarning(): void {
  // 检查用户是否选择了不再显示
  const dontShowSetting = localStorage.getItem('compatibility-warning-dismissed')
  if (dontShowSetting === 'true') {
    return
  }
  
  // 生成兼容性报告
  compatibilityReport.value = browserCompatibility.generateCompatibilityReport()
  
  // 判断是否需要显示警告
  if (browserCompatibility.shouldShowCompatibilityWarning()) {
    showWarning.value = true
  }
}

/**
 * 关闭警告对话框
 */
function dismissWarning(): void {
  showWarning.value = false
  
  if (dontShowAgain.value) {
    localStorage.setItem('compatibility-warning-dismissed', 'true')
  }
}

/**
 * 处理遮罩层点击事件
 * 点击遮罩层时关闭对话框
 */
function handleOverlayClick(): void {
  dismissWarning()
}

/**
 * 继续使用当前浏览器
 * 应用所有可用的降级方案后继续
 */
function continueAnyway(): void {
  // 应用降级方案
  browserCompatibility.applyAllFallbacks()
  
  // 显示提示信息
  console.log('已应用浏览器兼容性降级方案，游戏功能可能受限')
  
  dismissWarning()
}

/**
 * 刷新页面
 * 让用户有机会使用其他浏览器
 */
function refreshPage(): void {
  window.location.reload()
}

// 组件挂载时检查兼容性
onMounted(() => {
  // 延迟检查，确保页面完全加载
  setTimeout(checkShouldShowWarning, 1000)
})
</script>

<style scoped lang="scss">
.compatibility-warning-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.7);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 10000;
  padding: 1rem;
  backdrop-filter: blur(4px);
}

.compatibility-warning-modal {
  background: white;
  border-radius: 12px;
  box-shadow: 0 20px 40px rgba(0, 0, 0, 0.3);
  max-width: 600px;
  width: 100%;
  max-height: 90vh;
  overflow-y: auto;
  animation: modalSlideIn 0.3s ease-out;
}

@keyframes modalSlideIn {
  from {
    opacity: 0;
    transform: translateY(-20px) scale(0.95);
  }
  to {
    opacity: 1;
    transform: translateY(0) scale(1);
  }
}

.warning-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 1.5rem 2rem 1rem;
  border-bottom: 1px solid #e5e7eb;
}

.warning-title {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  margin: 0;
  font-size: 1.25rem;
  font-weight: 600;
  color: #dc2626;
}

.warning-icon {
  font-size: 1.5rem;
}

.close-button {
  background: none;
  border: none;
  font-size: 1.5rem;
  color: #6b7280;
  cursor: pointer;
  padding: 0.25rem;
  border-radius: 4px;
  transition: all 0.2s;
  
  &:hover {
    background: #f3f4f6;
    color: #374151;
  }
}

.warning-content {
  padding: 1.5rem 2rem;
  
  h3 {
    margin: 0 0 0.75rem 0;
    font-size: 1rem;
    font-weight: 600;
    color: #374151;
  }
  
  > div:not(:last-child) {
    margin-bottom: 1.5rem;
  }
}

.browser-info {
  p {
    margin: 0;
    color: #6b7280;
    line-height: 1.6;
  }
}

.compatibility-level {
  .level-indicator {
    display: flex;
    align-items: center;
    gap: 1rem;
  }
  
  .level-text {
    font-weight: 600;
    min-width: 60px;
    
    .level-excellent & { color: #059669; }
    .level-good & { color: #0891b2; }
    .level-fair & { color: #d97706; }
    .level-poor & { color: #dc2626; }
  }
  
  .level-bar {
    flex: 1;
    height: 8px;
    background: #e5e7eb;
    border-radius: 4px;
    overflow: hidden;
  }
  
  .level-fill {
    height: 100%;
    border-radius: 4px;
    transition: width 0.3s ease;
    
    .level-excellent & { background: #059669; }
    .level-good & { background: #0891b2; }
    .level-fair & { background: #d97706; }
    .level-poor & { background: #dc2626; }
  }
}

.warnings-section,
.recommendations-section {
  ul {
    margin: 0;
    padding-left: 1.25rem;
  }
  
  li {
    color: #6b7280;
    line-height: 1.6;
    margin-bottom: 0.5rem;
    
    &:last-child {
      margin-bottom: 0;
    }
  }
}

.warnings-list li {
  color: #dc2626;
}

.recommendations-list li {
  color: #0891b2;
}

.supported-browsers {
  .browser-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(120px, 1fr));
    gap: 0.75rem;
  }
  
  .browser-item {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    padding: 0.75rem;
    background: #f9fafb;
    border-radius: 8px;
    font-size: 0.875rem;
    color: #374151;
  }
  
  .browser-icon {
    font-size: 1.25rem;
  }
}

.warning-actions {
  display: flex;
  gap: 1rem;
  padding: 1rem 2rem;
  border-top: 1px solid #e5e7eb;
  justify-content: flex-end;
}

.action-button {
  padding: 0.75rem 1.5rem;
  border-radius: 8px;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.2s;
  border: none;
  
  &.primary {
    background: #3b82f6;
    color: white;
    
    &:hover {
      background: #2563eb;
    }
  }
  
  &.secondary {
    background: #f3f4f6;
    color: #374151;
    
    &:hover {
      background: #e5e7eb;
    }
  }
}

.warning-footer {
  padding: 1rem 2rem;
  border-top: 1px solid #e5e7eb;
  background: #f9fafb;
  border-radius: 0 0 12px 12px;
}

.dont-show-again {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  font-size: 0.875rem;
  color: #6b7280;
  cursor: pointer;
  
  input[type="checkbox"] {
    margin: 0;
  }
}

// 移动端适配
@media (max-width: 640px) {
  .compatibility-warning-overlay {
    padding: 0.5rem;
  }
  
  .warning-header,
  .warning-content,
  .warning-actions,
  .warning-footer {
    padding-left: 1rem;
    padding-right: 1rem;
  }
  
  .warning-actions {
    flex-direction: column;
    
    .action-button {
      width: 100%;
    }
  }
  
  .browser-grid {
    grid-template-columns: 1fr 1fr;
  }
}
</style>